package UI.Table;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.text.View;
import javax.swing.plaf.basic.BasicHTML;

//@author A0103535R
/******************** Abstract Class DSTaskTableCellTransparentLabel *******************/
public class DSTaskTableCellTransparentLabel extends JLabel {
	private static final long serialVersionUID = -5590205476792513894L;
	
	public static final int MAXIMUM_WIDTH = 300;
	public static final int TEXT_LAYOUT_GAP_X = 2;	
	private static final int TEXT_LAYOUT_WIDTH_GAP = 65;
	
	private int _maxWidth = 300;
	private int _width = 0, _height = 0;
	private String _textString = "";
	
	/**
	 * This is the constructor of DSTaskTableCellTransparentLabel
	 */
	public DSTaskTableCellTransparentLabel(String text) {
		super();
		setTextString(text);
		text = String.format("<html><p style=\"width:%dpx;\">%s</p><html>", _maxWidth-TEXT_LAYOUT_WIDTH_GAP, text);
		setText(text);
		setBackground(new Color(0, 0, 0, 0));
		setForeground(new Color(230, 230, 230, 255));
		setOpaque(false);
		setVisible(true);
	}
	
	/**
	 * This function is used to get the width of label
	 * 
	 * @return the width
	 */
	public int getWidth(){
		return _width;
	}

	/**
	 * This function is used to get the height of label
	 * 
	 * @return the height
	 */
	public int getHeight(){
		return _height;
	}
		
	/**
	 * This function is used to resize label based on its text
	 */
	public void resize() {
		View v = BasicHTML.createHTMLView(this, this.getText());
		v.setSize(_maxWidth, Integer.MAX_VALUE);
		int h = (int) v.getMinimumSpan(View.Y_AXIS);
		_width = _maxWidth;
		_height = h;
		setPreferredSize(new Dimension(_maxWidth, h));
		setMinimumSize(new Dimension(_maxWidth, h));
	}
	
	/**
	 * This function is used to resize label based on the maximum length provided.
	 * 
	 * @param maxlength
	 * 			maximum width of cell.
	 */
	public void resize(int maxlength) {
		setText(String.format("<html><p style=\"width:%dpx;\">%s</p><html>", maxlength-TEXT_LAYOUT_WIDTH_GAP, _textString));
		_maxWidth = maxlength;
		resize();
	}
	
	/**
	 * This function is used to set text to textString.
	 * 
	 * @param text
	 * 			text to set for textString
	 */
	private void setTextString(String text){
		_textString = text;
	}
}
